package com.sc.common.controller;

import com.sc.common.pojo.LoginType;
import com.sc.common.shiro.token.PersonalUsernamePasswordToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class AdministratorController {
    private String loginType = LoginType.ADMINISTRATOR.getLoginType();

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        String mess;
        Subject subject = SecurityUtils.getSubject();
        PersonalUsernamePasswordToken token = new PersonalUsernamePasswordToken(username, password, loginType);
        try {
            subject.login(token);
            return "show";
        } catch (UnknownAccountException e) {
            mess = "用户名不存在";
        } catch (IncorrectCredentialsException e) {
            mess = "密码输入错误";
        } catch (LockedAccountException e) {
            mess = "用户名已经冻结";
        } catch (AuthenticationException e) {
            mess = "用户名密码输入错误";
        }
        model.addAttribute("mess", mess);
        model.addAttribute("loginType", "admin");
        return "login";
    }
}
